package com.sgb.kjwl.view.ui.adapter.transport;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;


import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.CarriageInfo;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;


import java.util.List;

/***
 * 承运需求（竞价找车）列表adapter
 */
public class CarriageInfoListAdapter extends BaseRecycleAdapter<CarriageInfo> {
    public CarriageInfoListAdapter(Context context, List<CarriageInfo> datas) {
        super(context,R.layout.item_carrier_list, datas);
    }


    @Override
    public void convert(ItemViewHolder holder, CarriageInfo carriageInfo) {
        holder.setBinding(BR.viewModel, carriageInfo);
        TextView title_view=holder.getItem().findViewById(R.id.title_view);
        TextView tv_distance = holder.getItem().findViewById(R.id.tv_distance);
        TextView tiem_view=holder.getItem().findViewById(R.id.tiem_view);
        TextView price_view=holder.getItem().findViewById(R.id.price_view);
        TextView tv_city=holder.getItem().findViewById(R.id.tv_city);
        tv_city.setText(carriageInfo.getShipCityName() + " " + carriageInfo.getShipDistrictName() + " ➞ " + carriageInfo.getDestCityName() + " " + carriageInfo.getDestDistrictName());
        StringBuilder strBuilder = new StringBuilder();
        if(!TextUtils.isEmpty(carriageInfo.getCarNumber())){
            strBuilder.append(carriageInfo.getCarNumber());
        }
        if(!TextUtils.isEmpty(carriageInfo.getCarTypeName())){
            strBuilder.append("\u0020");
            strBuilder.append(carriageInfo.getCarTypeName());
        }

        if(!TextUtils.isEmpty(carriageInfo.getCarLenName())){
            strBuilder.append("\u0020");
            strBuilder.append(carriageInfo.getCarLenName());
        }
        if(!TextUtils.isEmpty(carriageInfo.getWeight())){
            strBuilder.append("\u0020");
            strBuilder.append(NumberUtil.trim0(carriageInfo.getWeight())+"吨");
        }
        if(!TextUtils.isEmpty(carriageInfo.getHeightHurdle())){
            strBuilder.append("\u0020栏高"+carriageInfo.getHeightHurdle());
            strBuilder.append("米");
        }
        if(carriageInfo.getIsDump()!=null){
            strBuilder.append("/");
            strBuilder.append(getIsDumpStr(carriageInfo.getIsDump()));
        }
        title_view.setText(strBuilder.toString());
        tiem_view.setText("装货时间:"+ DateUtils.stampToDay4(String.valueOf(carriageInfo.getShipmentStartTime()))+" - "+ DateUtils.stampToDay4(String.valueOf(carriageInfo.getShipmentEndTime())));
        if(TextUtils.isEmpty(carriageInfo.getDistance())){
            tv_distance.setText("");
        }else{
            tv_distance.setText( Double.valueOf(carriageInfo.getDistance())<1000?"1km以内": StringUtil.fromatUpDistance(carriageInfo.getDistance())+"km");
        }
        price_view.setText(NumberUtil.trim0(carriageInfo.getFreight())+"元/吨");
        holder.getItem().findViewById(R.id.call_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    VerificationCodeLoginActivity.start(context);
                }else{
                    if(!TextUtils.isEmpty(carriageInfo.getContrctsNumber())) {
                        new RoundDialog.Builder(context)
                                .title("确认呼叫")
                                .content(carriageInfo.getContrctsNumber())
                                .confirm("确定")
                                .cancelable(false)
                                .confirmCallback(view -> {
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + carriageInfo.getContrctsNumber());
                                    intent.setData(data);
                                    context.startActivity(intent);
                                })
                                .show();
                    }else{
                        ToastUtil.show(context,"无电话号码");
                    }
                }
            }
        });

    }
    public String getIsDumpStr(Integer isdump){
        switch (isdump){
            case 0:
                return "非自卸";
            case 1:
                return "自卸";
            default:
               return "";
        }

    }
}
